package com.kaoshi.myweb.snippet.dynamic.compile;

import com.kaoshi.myweb.dependent.entity.DynaicCompileParent;

/**
 * Created by Administrator on 2017/2/25.
 */
public class DynamicCompileTest {
	public static void main(String[] args) throws Exception {
		test();
		test();
	}

	private static void test() throws IllegalAccessException, InstantiationException {
		String fullName = "DynaClass";
		StringBuilder src = new StringBuilder();
		src.append("public class DynaClass extends com.kaoshi.myweb.dependent.entity.DynaicCompileParent {\n");
		src.append("    public String text; \n");
		src.append("    public String toString() {\n");
		src.append("        return \"Hello, I am \" + ");
		src.append("this.getClass().getSimpleName();\n");
		src.append("    }\n");
		src.append("}\n");

		System.out.println(src);
		DynamicEngine de = DynamicEngine.getInstance();
		//动态编译类继承jar包，方便设置，修改值
		de.addClassPath("F://specialty//GitRepository//myweb//snippet//lib//myweb-dependent-0.0.1-SNAPSHOT.jar");
		DynaicCompileParent dynaicCompileParent =  (DynaicCompileParent)de.javaCodeToObject(fullName,src.toString());
		System.out.println(dynaicCompileParent);
		dynaicCompileParent.setFieldValue("text", "haha");
		System.out.println(dynaicCompileParent.getFieldValue("text"));
	}
}
